package com.org.csu.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.org.csu.mapper.accountMapper;
import com.org.csu.mapper.keywordMapper;
import com.org.csu.mapper.recordMapper;
import com.org.csu.pojo.Keyword;
import com.org.csu.pojo.KeywordDTO;
import com.org.csu.pojo.Record;
import com.org.csu.pojo.User;
import com.org.csu.service.accountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class accountServiceImpl implements accountService {
    @Autowired
    private accountMapper accountmapper;
    @Autowired
    private recordMapper recordmapper;
    @Autowired
    private keywordMapper keywordmapper;
    @Override
    public String isRightOfAccount(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username).eq("password",password);
        User user = accountmapper.selectOne(queryWrapper);
        if (user != null)
            return "登录成功";
        else
            return "用户名或密码错误！";
    }
    @Override
    public Integer getUserIdbyName(String username) {
        User user =accountmapper.selectByUsername(username);
        return user.getUserId();
    }
    @Override
    public String isExistUserName(String username) {
        User user =accountmapper.selectByUsername(username);
        if (user != null)
            return "exist";
        else
            return "no exist";
    }

    @Override
    public String setUserInfo(User user) {
        if (isExistUserName(user.getUsername()) == "exist")
            return "用户名已存在！";
        accountmapper.insert(user);
        return "注册成功";
    }

    @Override
    public User getuserInfoByUserId(Integer userId) {

        return accountmapper.selectById(userId);
    }
    @Override
    public List<Record> getrecordInfoByUserId(Integer userId) {
        return recordmapper.findByUserId(userId);
    }

    @Override
    public String recharge(Integer userId,Integer permission){
        User user =accountmapper.selectById(userId);
        if (user.getPermission() == permission){
            return "充值失败";
        }
        user.setPermission(permission);
        System.out.println(user.getPermission());
        return "充值成功";
        }
    @Override
    public Integer getrecordIdByinformations(Integer userId,String keyword,String date){
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.like("keyword", keyword);
        queryWrapper.eq("date", date);
        Record record = recordmapper.selectOne(queryWrapper);
        if (record != null) {
            return record.getRecordId();
        } else {
            return null;
        }
    }
    @Override
    public KeywordDTO getinformationsByrecordId(Integer recordId){
        Keyword keyword = keywordmapper.selectById(recordId);
        if (keyword != null) {
            return new KeywordDTO(keyword.getMidword(), keyword.getCompword(), keyword.getRate(), keyword.getTrend());
        } else {
            return null;
        }
    }
}
